package utils

import (
	"encoding/json"
	"fmt"
)

// 序列化结构
func SerializeStruct(v interface{})string {

	b, err := json.Marshal(v)
	if err != nil {
		fmt.Println("Marshal err : ", err.Error())
		return ""
	}
	return string(b)
	//fmt.Println("Marshal json : ", string(b))
}

// 序列化Map
func SerializeMap(v interface{}) {
	b, err := json.Marshal(v)
	if err != nil {
		fmt.Println("Marshal err : ", err.Error())
		return
	}
	fmt.Println("Marshal json : ", string(b))
}


// 反序列化结构体
func DeSerializeStruct()  {
	//jsonString := `{"name":"localhost","ip":"127.0.0.1","port":8080}`
	//server := new(Server)
	//err := json.Unmarshal([]byte(jsonString),&server)
	//if err != nil{
	//	fmt.Println("Unmarshal err : ",err.Error())
	//}
	//fmt.Println("Unmarshal struct : ",server)
}

func DeSerializeMap()  {
	jsonString := `{"name":"localhost","ip":"127.0.0.1","port":8080}`
	server := make(map[string]interface{})
	err := json.Unmarshal([]byte(jsonString),&server)
	if err!=nil{
		fmt.Println("Unmarshal err : ",err.Error())
	}
	fmt.Println("Unmarshal map : ",server)
}